39F - Pacifist frogs - CodeForces Solution


implementation *1300

Please click on ads to support us..

Python Code:

def read_numbers():
    return [int(x) for x in input().split()]

def main():
    n, m, k = read_numbers()
    d = read_numbers()
    h = read_numbers()
    res = []
    min_count = 10 ** 9

    for frog in enumerate(d, start=1):
        curr_count = 0
        for hill in h:
            if hill % frog[1] == 0:
                curr_count += 1
        if curr_count < min_count:
            min_count = curr_count
            res = [frog[0]]
        elif curr_count == min_count:
            res.append(frog[0])
        
    print(len(res))
    print(' '.join([str(x) for x in res]))

if __name__ == '__main__':
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define sort(x) sort(all(x))
#define uniq(x) unique(all(x))
template<typename _Tp>
istream& operator >> (istream& i, vector<_Tp>& v){
  for(_Tp& x : v)
    i >> x;
  return i;
}
template<typename _Tp>
ostream& operator << (ostream& o, vector<_Tp>& v){
  for(_Tp& x : v)
    o << x << ' ';
  return o;
}
signed main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int tt=1;
  #ifdef MULTITEST
  cin >> tt;
  #endif
  while(tt--){
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> d(m), pos(k);
    cin >> d >> pos;
    
    int best=1001;
    vector<int> ans;
    for(int i=0; i<m; ++i){
      int di=d[i];

      int cnt=0;
      for(int j=0; j<k; ++j)
        if(pos[j]%di==0)
          ++cnt;

      if(cnt < best){
        best=cnt;
        ans.clear();
      }
      if(best == cnt)
        ans.emplace_back(i+1);
    }
    cout << (int)ans.size() << '\n' << ans;
    if(tt)  cout << endl;
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK